
package com.probiz.estore.product.dao.impl;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.probiz.estore.common.helper.ConfigUtil;
import com.probiz.estore.common.model.product.StockNotify;
import com.probiz.estore.common.util.DateUtil;
import com.probiz.estore.common.util.SearchUtil;
import com.probiz.estore.core.dao.impl.HibernateGenericDaoImpl;
import com.probiz.estore.core.search.SearchCriteria;
import com.probiz.estore.product.dao.StockNotifyDao;
import com.probiz.estore.product.model.StockNotifySearchCriteria;

/**
 * Dao implementation for StockNotify.
 */
public class StockNotifyDaoImpl extends HibernateGenericDaoImpl<StockNotify>
		implements StockNotifyDao {

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.product.dao.StockNotifyDao#searchByCriteria(com.probiz.estore.product.model.StockNotifySearchCriteria,
	 *      com.probiz.estore.core.search.SearchCriteria)
	 */
	public List<StockNotify> searchByCriteria(StockNotifySearchCriteria stockNotifySearchCriteria,SearchCriteria searchCriteria) {
		StringBuffer hqlsb = new StringBuffer(100);
		List<Object> paramList = new ArrayList<Object>();
		// start:构建HQL语句
		//特别注意，因为customer可为空，所以在查询咨询人名称时候必须加上左外连接。
		hqlsb
				.append("select s from StockNotify s left join s.customer as u where 1=1 ");
		// -begin:关键词
		if (StringUtils.isNotBlank(stockNotifySearchCriteria.getKeywords())) {
	           String keywords = SearchUtil.getSqlQueryValue(stockNotifySearchCriteria.getKeywords());
	           hqlsb.append(" and (");	           
	           hqlsb.append("    s.product.productName like ? ");
	           hqlsb.append(" or s.product.keywords like ? ");
	           hqlsb.append(" or s.product.productCode like ? ");
	           hqlsb.append(" or s.product.category.categoryName like ? ");
	           hqlsb.append(" or s.product.category.keywords like ? ");
	           hqlsb.append(" )");
	           for (int i = 0; i < 5; i++) {
	               paramList.add("%" + keywords + "%");
	           }
	       }
		// -end:关键词
		// -start:产品编码
		if (StringUtils.isNotBlank(stockNotifySearchCriteria.getProductCode())) {
			hqlsb.append(" and s.product.productCode = ?");
			paramList.add(stockNotifySearchCriteria.getProductCode());
		}
		// -end:产品编码
		//-start:登记开始时间
		if(StringUtils.isNotBlank(stockNotifySearchCriteria.getStartCreateTime())){
			try {
				Date date = DateUtil.convertStringToDate(ConfigUtil.getInstance().getDatePattern(),stockNotifySearchCriteria.getStartCreateTime());
				Calendar calender = Calendar.getInstance();
				calender.setTime(date);
				calender.set(Calendar.HOUR_OF_DAY, 0);
				calender.set(Calendar.MINUTE, 0);
				calender.set(Calendar.SECOND, 0);
				hqlsb.append(" and s.createTime >= ?");
				paramList.add(calender.getTime());
			} catch (ParseException e) {
				e.printStackTrace();
			}
        }
        //-end:登记开始时间
        //-start:登记结束时间
		if(StringUtils.isNotBlank(stockNotifySearchCriteria.getEndCreateTime())){
			try {
				Date date = DateUtil.convertStringToDate(ConfigUtil.getInstance().getDatePattern(),stockNotifySearchCriteria.getEndCreateTime());
				Calendar calender = Calendar.getInstance();
				calender.setTime(date);
				calender.add(Calendar.DAY_OF_YEAR, 1);
				calender.set(Calendar.HOUR_OF_DAY, 0);
				calender.set(Calendar.MINUTE, 0);
				calender.set(Calendar.SECOND, 0);
				hqlsb.append(" and s.createTime <= ?");
				paramList.add(calender.getTime());
			} catch (ParseException e) {
				e.printStackTrace();
			}
        }
        //-end:登记结束时间
		//-start:通知开始时间
        if(StringUtils.isNotBlank(stockNotifySearchCriteria.getStartInformTime())){
			try {
				Date date = DateUtil.convertStringToDate(ConfigUtil.getInstance().getDatePattern(),stockNotifySearchCriteria.getStartInformTime());
				Calendar calender = Calendar.getInstance();
				calender.setTime(date);
				calender.set(Calendar.HOUR_OF_DAY, 0);
				calender.set(Calendar.MINUTE, 0);
				calender.set(Calendar.SECOND, 0);
				hqlsb.append(" and s.informTime >= ?");
				paramList.add(calender.getTime());
			} catch (ParseException e) {
				e.printStackTrace();
			}
        }
        //-end:通知开始时间
        //-start:通知结束时间
        if(StringUtils.isNotBlank(stockNotifySearchCriteria.getEndInformTime())){
			try {
				Date date = DateUtil.convertStringToDate(ConfigUtil.getInstance().getDatePattern(),stockNotifySearchCriteria.getEndInformTime());
				Calendar calender = Calendar.getInstance();
				calender.setTime(date);
				calender.add(Calendar.DAY_OF_YEAR, 1);
				calender.set(Calendar.HOUR_OF_DAY, 0);
				calender.set(Calendar.MINUTE, 0);
				calender.set(Calendar.SECOND, 0);
				hqlsb.append(" and s.informTime <= ?");
				paramList.add(calender.getTime());
			} catch (ParseException e) {
				e.printStackTrace();
			}
        }
        //-end:通知结束时间
        //-start:顾客名称
       if (StringUtils.isNotBlank(stockNotifySearchCriteria.getCustomerName())) {
    	   hqlsb.append(" and (s.customerName like ?");
    	   hqlsb.append(" or u.firstname like ?)");
    	   for (int i = 0; i < 2; i++) {
    		   paramList.add("%" + stockNotifySearchCriteria.getCustomerName() + "%");
    	   }	   
       }
       //-end:顾客名称
       //-start:状态:通知状态
       if (null != stockNotifySearchCriteria.getInformStatus()) {
           hqlsb.append(" and s.informStatus = ?");
           paramList.add(stockNotifySearchCriteria.getInformStatus());
       }
       //-end:状态:通知状态
        
		// end:构建HQL语句
		searchCriteria.setHql(hqlsb.toString());
		searchCriteria.removeParamValues();
		for (Object object : paramList) {
			searchCriteria.addParamValue(object);
		}
		List<StockNotify> stockNotifyList = searchByCriteria(searchCriteria);
		return stockNotifyList;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.product.dao.StockNotifyDao#countUninformStockNotifyTotal()
	 */

	public Long countUninformStockNotifyTotal() {
		return this.countByHql("from StockNotify s where s.informStatus is null or s.informStatus !=1");
	}
}
